package com.bit.bit_forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
public class AppResult<T> {

    //这个注解表示不论什么情况下,都参与JSON序列化
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private long code;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    //构造方法:
    public AppResult(){}

    public AppResult(long code, String message) {
        this.code = code;
        this.message = message;
    }

    public AppResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
    //    常用方法
//      成功
     * @return
     */
    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }
//    成功,返回成功数据
    public static <T> AppResult<T> success(T data){
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }

    /*
//    失败
    返回描述信息
     */
    public static <T> AppResult<T> failed(String message){
        return new AppResult<>(ResultCode.FAILED.getCode(),message);


    }
    /*
//    失败
    返回异常信息
     */
    public static <T> AppResult<T> failed(T data){
        return new AppResult<>(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage(),data);
    }
    /*
//    失败
    返回异常信息
     */
    public static <T> AppResult<T> failed(ResultCode resultCode){
        return new AppResult<>(resultCode.getCode(),resultCode.getMessage());
    }


}
